package com.ruoyi.web.controller.kaoyan;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.kaoyan.domain.EducationCategory;
import com.ruoyi.kaoyan.domain.EducationFileInfo;
import com.ruoyi.kaoyan.domain.EducationForum;
import com.ruoyi.kaoyan.service.EducationCategoryService;
import com.ruoyi.kaoyan.service.EducationFileInfoService;
import com.ruoyi.system.service.ISysFileInfoService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.web.controller.kaoyan.vo.FileInfoVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 后台分享资料管理
 * @author admin
 */
@Api(tags = "后台分享资料审核管理", value = "后台分享资料审核管理")
@RestController
@RequestMapping("/education/file")
public class EducationFileController extends BaseController {

    @Autowired
    private EducationFileInfoService fileInfoService;
    @Autowired
    private EducationCategoryService categoryService;
    @Autowired
    private ISysFileInfoService sysFileInfoService;
    @Autowired
    private ISysUserService userService;

    @ApiOperation("资料列表")
    @PostMapping("/list")
    public AjaxResult fileList(Long categoryId, String name){
        LambdaQueryWrapper<EducationFileInfo> wrapper = new LambdaQueryWrapper<EducationFileInfo>().like(StringUtils.isNotBlank(name), EducationFileInfo::getName, name);
        if (StringUtils.isNotNull(categoryId)){
            wrapper.eq(true, EducationFileInfo::getCategoryId, categoryId);
        }
        Page<EducationFileInfo> page = fileInfoService.page(startPagePlus(), wrapper);
        List<EducationFileInfo> fileInfoList = page.getRecords();
        Set<Long> categoryIdSet = fileInfoList.stream().map(EducationFileInfo::getCategoryId).collect(Collectors.toSet());
        Set<Long> userIdSet = fileInfoList.stream().map(EducationFileInfo::getUserId).collect(Collectors.toSet());
        List<EducationCategory> educationCategories = categoryService.list(new LambdaQueryWrapper<EducationCategory>()
                .eq(StringUtils.isNotEmpty(categoryIdSet), EducationCategory::getId, categoryIdSet));
        List<SysUser> educationUsers = userService.list(new LambdaQueryWrapper<SysUser>()
                .select(SysUser::getUserId, SysUser::getUserName).in(StringUtils.isNotEmpty(userIdSet), SysUser::getUserId, userIdSet));
        Map<Long, String> categoryNameMap = educationCategories.stream().collect(Collectors.toMap(EducationCategory::getId, EducationCategory::getName));
        Map<Long, String> userNameMap = educationUsers.stream().collect(Collectors.toMap(SysUser::getUserId, SysUser::getUserName));
        List<FileInfoVo> infoVoList = fileInfoList.stream().map(e -> new FileInfoVo(e, categoryNameMap, userNameMap)).collect(Collectors.toList());
        return AjaxResult.ok().page(page, infoVoList).build();
    }

    @ApiOperation("资料文件详情")
    @PostMapping("/detail")
    public AjaxResult detail(Long id) {
        EducationFileInfo fileInfo = fileInfoService.getById(id);
        String fileGroupId = fileInfo.getFileGroupId();
        String filePath = sysFileInfoService.getFilePathByGroupId(fileGroupId);
        EducationFileInfo educationFile = EducationFileInfo.builder()
                .filePath(filePath)
                .build();
        return AjaxResult.ok().proPut("detail", educationFile).build();
    }

    @ApiOperation("资料审核")
    @PostMapping("/verFlag")
    public AjaxResult verFlag(Long id, Integer flag){
        EducationFileInfo fileInfo = fileInfoService.getById(id);
        if (StringUtils.checkObjAllFieldsIsNull(fileInfo)){
            return AjaxResult.error("资料不存在");
        }
        fileInfo.setVerFlag(flag);
        boolean update = fileInfoService.updateById(fileInfo);
        return toAjax(update);
    }
}
